//-----------------------------------------------------------------------
//  This file is part of the Microsoft Code Samples.
// 
//  Copyright (C) Microsoft Corporation.  All rights reserved.
// 
//  This source code is intended only as a supplement to Microsoft
//  Development Tools and/or on-line documentation.  See these other
//  materials for detailed information regarding Microsoft code samples.
// 
//  THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
//  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//  PARTICULAR PURPOSE.
//-----------------------------------------------------------------------

using System;
using System.Threading;
using System.Data;
using System.Xml;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Collections;
using System.Text;
using System.IO;

namespace Microsoft.Samples.SqlServer
{
    /// <summary>
    /// This class safely reads from a data reader by performing null checks.
    /// </summary>
    public class Utility
    {
        #region Get String From Reader
        /*
         * This method safely gets a string from a reader by performing null checks.
         */
        public static string GetStringFromReader(SqlDataReader sdr, string colName)
        {
            string result = "";
            int index = sdr.GetOrdinal(colName);

            if (sdr.IsDBNull(index))
            {
                // The column returns DBNull
            }
            else 
            {
                result =sdr.GetString(index);
            }

            return result;
        }

        #endregion

        #region Get Guid From Reader
        /*
         * This method safely gets a Guid from a reader by performing null checks.
         */
        public static Guid GetGuidFromReader(SqlDataReader sdr, string colName)
        {
            Guid result = Guid.Empty;
            int index = sdr.GetOrdinal(colName);

            if (sdr.IsDBNull(index))
            {
                // The column returns DBNull
            }
            else 
            {
                result =sdr.GetGuid(index);
            }

            return result;
        }

        #endregion

        #region Get Int From Reader
        /*
         * This method safely gets a int from a reader by performing null checks.
         */
        public static int GetIntFromReader(SqlDataReader sdr, string colName)
        {
            int result = 0;
            int index = sdr.GetOrdinal(colName);

            if (sdr.IsDBNull(index))
            {
                // The column returns DBNull
            }
            else
            {
                result = sdr.GetInt32(index);
            }

            return result;
        }
        #endregion

        #region Get string From Stream
        /*
         * This method safely gets a int from a reader by performing null checks.
         */
        public static string GetStringFromStream(Stream body)
        {
            string result = null;

            if (body != null)
            {
                body.Seek(0, SeekOrigin.Begin);
                TextReader reader = new StreamReader(body, Encoding.ASCII);
                result = reader.ReadToEnd();
            }

            return result;
        }
        #endregion
    }
}
